package com.ybbase.framework.service.flow.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ybbase.framework.mapper.flow.FlowConfigMapper;
import com.ybbase.framework.mapper.flow.NodeActionMapper;
import com.ybbase.framework.mapper.flow.NodeUsergroupMapper;
import com.ybbase.framework.mapper.system.OrganUserMapper;
import com.ybbase.framework.mapper.system.OrganizationMapper;
import com.ybbase.framework.mapper.system.UserMapper;
import com.ybbase.framework.model.po.flow.FlowConfig;
import com.ybbase.framework.model.po.flow.NodeAction;
import com.ybbase.framework.model.po.flow.NodeConfig;
import com.ybbase.framework.mapper.flow.NodeConfigMapper;
import com.ybbase.framework.model.po.flow.NodeUsergroup;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.model.po.system.User;
import com.ybbase.framework.service.flow.NodeConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 节点配置表 服务实现类
 * </p>
 *
 * @author cg
 * @since 2019-12-04
 */
@Service
public class NodeConfigServiceImpl extends ServiceImpl<NodeConfigMapper, NodeConfig> implements NodeConfigService {

    @Autowired
    NodeConfigMapper nodeConfigMapper;
    @Autowired
    NodeUsergroupMapper nodeUsergroupMapper;
    @Autowired
    NodeActionMapper nodeActionMapper;
    @Autowired
    FlowConfigMapper flowConfigMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrganizationMapper organizationMapper;
    /**
     * 根据流程编码获取流程节点用户配置
     * @param flowCode 流程编码
     * @param nodeCode 节点编码
     * @return
     * @author cg
     * @date 2019-11-07 15:36
     * @since 4.0
     */
    @Override
    public List<NodeUsergroup> getUserByFlowNode(String flowCode, String nodeCode) {
        QueryWrapper<NodeUsergroup> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(NodeUsergroup::getFlowCode, flowCode);
        wrapper.lambda().eq(NodeUsergroup::getNodeCode, nodeCode);
        return nodeUsergroupMapper.selectList(wrapper);
    }

    /**
     * 删除流程编码获取流程节点用户配置
     *
     * @param id 节点用户配置编码
     * @return
     * @author cg
     * @date 2019-11-12 18:50
     * @since 4.0
     */
    @Override
    public void removeUserGroup(Integer id) throws Exception {
        nodeUsergroupMapper.deleteById(id);
    }

    /**
     * 保存流程节点配置
     *
     * @param nodeConfig 流程节点配置
     * @return
     * @author cg
     * @date 2019-11-13 15:12
     * @since 4.0
     */
    @Override
    public void saveNodeConfig(NodeConfig nodeConfig) throws Exception{
        QueryWrapper<FlowConfig> flowConfigQueryWrapper = new QueryWrapper<>();
        flowConfigQueryWrapper.lambda().eq(FlowConfig::getFlowCode, nodeConfig.getFlowCode());
        FlowConfig flowConfig = flowConfigMapper.selectList(flowConfigQueryWrapper).get(0);
        nodeConfig.setBusinessName(flowConfig!=null?flowConfig.getFlowName():"");
        nodeConfigMapper.insert(nodeConfig);
        //saveConfigUserGroup(nodeConfig);
        //saveConfigAction(nodeConfig);
    }
    /**
     * 编辑流程节点配置
     * @param nodeConfig 流程节点配置
     * @return
     * @author cg
     * @date 2019-11-13 17:58
     * @since 4.0
     */
    @Override
    public void editNodeConfig(NodeConfig nodeConfig) throws Exception {
        nodeConfigMapper.updateById(nodeConfig);
        //saveConfigUserGroup(nodeConfig);
        //editConfigAction(nodeConfig);
    }
    /**
     * 保存流程业务用户节点
     * @param nodeId 节点ID
     * @param userId 用户ID
     * @return
     * @author cg
     * @date 2019-11-06 20:07
     * @since 4.0
     */
    @Override
    public void saveConfigUserGroup(Integer nodeId,Integer organId,Integer userId) throws Exception {
        NodeConfig nodeConfig = nodeConfigMapper.selectById(nodeId);
        Organization organization = organizationMapper.selectById(organId);
        NodeUsergroup nodeUsergroup = new NodeUsergroup();
        nodeUsergroup.setFlowCode(nodeConfig.getFlowCode());
        nodeUsergroup.setNodeCode(nodeConfig.getNodeCode());
        nodeUsergroup.setTaskUserCode(nodeConfig.getNodeCode()+"Users");
        nodeUsergroup.setOrgIds(organId+"");
        nodeUsergroup.setOrgFlowType(organization.getName());
        nodeUsergroup.setUserIds(userId+"");
        nodeUsergroupMapper.insert(nodeUsergroup);
    }

    /**
     * 保存流程业务节点上的方法
     * @param nodeConfig 节点配置
     * @return
     * @author cg
     * @date 2019-11-06 21:10
     * @since 4.0
     */
    @Override
    public void saveConfigAction(NodeConfig nodeConfig) throws Exception {
        this.saveGatewayActionItator(nodeConfig);
    }

    /**
     * 保存流程业务节点上的方法
     * @param nodeConfig 节点配置
     * @return
     * @author cg
     * @date 2019-11-06 21:10
     * @since 4.0
     */
    @Override
    public void editConfigAction(NodeConfig nodeConfig) throws Exception {
        QueryWrapper<NodeAction> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NodeAction::getFlowCode, nodeConfig.getFlowCode());
        queryWrapper.lambda().eq(NodeAction::getNodeCode, nodeConfig.getNodeCode());
        int i = nodeActionMapper.delete(queryWrapper);
        System.out.println("流程业务节点上的方法删除了"+i+"条");
        this.saveGatewayActionItator(nodeConfig);
    }


    /**
     * 根据节点编码查询用户节点数据
     *
     * @param config@return
     * @author cg
     * @date 2020-01-11 22:26
     * @since cloud2.0
     */
    @Override
    public List<User> getUserByNodeConfig(String userIds) {
        if(StringUtils.isNotEmpty(userIds)){
            String[] users = userIds.split(",");
            return userMapper.getUserByNodeConfig(users);
        }else {
            return new ArrayList<User>();
        }

    }
    /**
     * 根据节点编码删除用户节点数据
     * @param
     * @return
     * @author cg
     * @date 2020-01-11 22:26
     * @since cloud2.0
     */
    @Override
    public void removeByNodeId(Integer id) {
        NodeConfig nodeConfig = nodeConfigMapper.selectById(id);
        nodeConfigMapper.deleteById(id);
        UpdateWrapper<NodeUsergroup> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(NodeUsergroup::getFlowCode, nodeConfig.getFlowCode());
        updateWrapper.lambda().eq(NodeUsergroup::getNodeCode, nodeConfig.getNodeCode());
        nodeUsergroupMapper.delete(updateWrapper);
    }

    /**
     * 查询用户节点配置信息
     *
     * @param nw@return
     * @author cg
     * @date 2020-03-01 23:42
     * @since cloud2.0
     */
    @Override
    public List<NodeUsergroup> getNodeGroup(QueryWrapper<NodeUsergroup> nw) {
        return nodeUsergroupMapper.selectList(nw);
    }

    /**
     * 保存网数据
     * @param nodeConfig 节点配置
     * @return
     * @author cg
     * @date 2019-11-13 18:22
     * @since 4.0
     */
    private void saveGatewayActionItator(NodeConfig nodeConfig)  throws Exception{
        String[] strs = nodeConfig.getActionCodes();
        if(strs!=null&&strs.length>0){
            for(String a:strs){
                NodeAction g = new NodeAction();
                //g.setSort(a.getSort());
                g.setFlowCode(nodeConfig.getFlowCode());
                g.setNodeCode(nodeConfig.getNodeCode());
                g.setActionCode(a);
                nodeActionMapper.insert(g);
            }
        }
    }
}
